package com.h3c.nem.utils;

import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * @author chenyifan
 * @date 2022/4/27 14:17
 */
public class StrUtil {

    public static List<Long> toLongList(String str) {
        return Arrays.asList(toLongArray(str));
    }

    private static Long[] toLongArray(String str) {
        return toLongArray(",", str);
    }

    private static Long[] toLongArray(String split, String str) {
        if (StringUtils.isEmpty(str)) {
            return new Long[0];
        } else {
            String[] arr = str.split(split);
            Long[] longs = new Long[arr.length];

            for(int i = 0; i < arr.length; ++i) {
                Long v = toLong(arr[i], 0L);
                longs[i] = v;
            }

            return longs;
        }
    }

    private static long toLong(@Nullable final String str, final long defaultValue) {
        if (str == null) {
            return defaultValue;
        } else {
            try {
                return Long.valueOf(str);
            } catch (NumberFormatException var4) {
                return defaultValue;
            }
        }
    }


    public static List<String> toStrList(String str) {
        return Arrays.asList(toStrArray(str));
    }

    private static String[] toStrArray(String str) {
        return toStrArray(",", str);
    }

    private static String[] toStrArray(String split, String str) {
        return org.apache.commons.lang3.StringUtils.isBlank(str) ? new String[0] : str.split(split);
    }
}
